package com.zxj.flights.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.zxj.flights.common.util.PageInfoUtil;
import com.zxj.flights.users.dao.IAMovieDao;
import com.zxj.flights.users.dao.IANewsDao;
import com.zxj.flights.users.pojo.AMovie;
import com.zxj.flights.users.pojo.ANews;
import com.zxj.flights.users.service.IUserService;

/**
 * 用户相关的service实现类
 *
 */
@Service("userService")
public class UserServiceimpl implements IUserService
{
	@Resource
    private IANewsDao  newsDao;
	@Resource
    private IAMovieDao  movieDao;

	/** 新闻开始 */
	@Override
	public JSONObject saveOneANewsService(ANews news)
	{

		JSONObject resultJSON = new JSONObject();
		int res = this.newsDao.save(news);
		if (res > 0)
		{
			/**
			 * 成功
			 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", news.getId() + "");
			/* 影响条数 */
			dataJSON.put("effect", res + "");
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
		} else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneANewsService(ANews news)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.newsDao.update(news);
		if (res > 0)
		{
			/* 成功 */
			JSONObject dataJSON = new JSONObject();
			/* 影响条数 */
			dataJSON.put("effect", res + "");
			resultJSON.put("data", dataJSON);

			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
		} else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneANewsService(ANews news)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.newsDao.delete(news);
		if (res > 0)
		{
			/* 成功 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", news.getId() + "");
			/* 影响条数 */
			dataJSON.put("effect", res + "");
			resultJSON.put("data", dataJSON);

			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
		} else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}

	@Override
	public ANews findOneANewsService(Map<String, Object> condMap)
	{
		return this.newsDao.findOne(condMap);
	}

	@Override
	public List<ANews> findANewsCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if (pageInfoUtil != null)
		{
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			condMap.put("currentRecord", "0");
			condMap.put("pageSize", pageInfoUtil.getPageSize());

			List<ANews> newsList = this.newsDao.findCondList(condMap);
			/* ?如何取得总记录数,将总记录数放到news对象中的id属性中 */
			if (newsList.size() == 1)
			{
				/* List中肯定有一个元素,因为select count(*) 只返回一条结果 */
				pageInfoUtil.setTotalRecord(newsList.get(0).getId());
			}

			/* 再拿结果 */
			condMap.put("pageCount", "false");
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.newsDao.findCondList(condMap);
		} else
		{
			/* 直接查询,不分页 */
			return this.newsDao.findCondList(condMap);
		}
		/* 空的集合,不能返回null */
		/* return Collections.EMPTY_LIST; */
	}
	/** 新闻结束 */
	/** 电影开始 */
	@Override
	public JSONObject saveOneAMovieService(AMovie Movie)
	{

		JSONObject resultJSON = new JSONObject();
		int res = this.movieDao.save(Movie);
		if (res > 0)
		{
			/**
			 * 成功
			 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", Movie.getId() + "");
			/* 影响条数 */
			dataJSON.put("effect", res + "");
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
		} else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAMovieService(AMovie Movie)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.movieDao.update(Movie);
		if (res > 0)
		{
			/* 成功 */
			JSONObject dataJSON = new JSONObject();
			/* 影响条数 */
			dataJSON.put("effect", res + "");
			resultJSON.put("data", dataJSON);

			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
		} else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneAMovieService(AMovie Movie)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.movieDao.delete(Movie);
		if (res > 0)
		{
			/* 成功 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", Movie.getId() + "");
			/* 影响条数 */
			dataJSON.put("effect", res + "");
			resultJSON.put("data", dataJSON);

			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
		} else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}

	@Override
	public AMovie findOneAMovieService(Map<String, Object> condMap)
	{
		return this.movieDao.findOne(condMap);
	}

	@Override
	public List<AMovie> findAMovieCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		if (condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		if (pageInfoUtil != null)
		{
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			condMap.put("currentRecord", "0");
			condMap.put("pageSize", pageInfoUtil.getPageSize());

			List<AMovie> MovieList = this.movieDao.findCondList(condMap);
			/* ?如何取得总记录数,将总记录数放到Movie对象中的id属性中 */
			if (MovieList.size() == 1)
			{
				/* List中肯定有一个元素,因为select count(*) 只返回一条结果 */
				pageInfoUtil.setTotalRecord(MovieList.get(0).getId());
			}

			/* 再拿结果 */
			condMap.put("pageCount", "false");
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.movieDao.findCondList(condMap);
		} else
		{
			/* 直接查询,不分页 */
			return this.movieDao.findCondList(condMap);
		}
		/* 空的集合,不能返回null */
		/* return Collections.EMPTY_LIST; */
	}
	/** 电影结束 */
}
